1
|
|
|
var dgram = require('dgram') |
2
|
|
|
var util = require('util') |
3
|
|
|
var packet = require('dns-packet') |
4
|
|
|
var events = require('events') |
5
|
|
|
|
6
|
|
|
module.exports = DNS |
7
|
|
|
|
8
|
|
|
function DNS (opts) { |
9
|
|
|
if (!(this instanceof DNS)) return new DNS(opts) |
|
|
|
|
10
|
|
|
if (!opts) opts = {} |
|
|
|
|
11
|
|
|
|
12
|
|
|
events.EventEmitter.call(this) |
13
|
|
|
|
14
|
|
|
var self = this |
15
|
|
|
|
16
|
|
|
this.retries = opts.retries || 5 |
17
|
|
|
this.timeout = opts.timeout || 7500 |
18
|
|
|
this.destroyed = false |
19
|
|
|
this.inflight = 0 |
20
|
|
|
this.socket = opts.socket || dgram.createSocket('udp4') |
21
|
|
|
this._id = Math.ceil(Math.random() * 65535) |
22
|
|
|
this._ids = [] |
23
|
|
|
this._queries = [] |
24
|
|
|
this._interval = null |
25
|
|
|
this._triesArray = getTriesArray(this.retries) // default: [2, 4, 8, 16] = .5s, 1s, 2s, 4s |
26
|
|
|
|
27
|
|
|
this.socket.on('error', onerror) |
28
|
|
|
this.socket.on('message', onmessage) |
29
|
|
|
if (isListening(this.socket)) onlistening() |
|
|
|
|
30
|
|
|
else this.socket.on('listening', onlistening) |
31
|
|
|
this.socket.on('close', onclose) |
32
|
|
|
|
33
|
|
|
function onerror (err) { |
34
|
|
|
if (err.code === 'EACCES' || err.code === 'EADDRINUSE') self.emit('error', err) |
|
|
|
|
35
|
|
|
else self.emit('warning', err) |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
function onmessage (message, rinfo) { |
39
|
|
|
self._onmessage(message, rinfo) |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
function onlistening () { |
43
|
|
|
var timeSlices = self._triesArray.reduce(add, 0) |
44
|
|
|
self._interval = setInterval(ontimeout, Math.round(self.timeout / timeSlices)) |
45
|
|
|
self.emit('listening') |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
function onclose () { |
49
|
|
|
self.emit('close') |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
function ontimeout () { |
53
|
|
|
self._ontimeout() |
54
|
|
|
} |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
util.inherits(DNS, events.EventEmitter) |
58
|
|
|
|
59
|
|
|
DNS.RECURSION_DESIRED = DNS.prototype.RECURSION_DESIRED = packet.RECURSION_DESIRED |
60
|
|
|
DNS.RECURSION_AVAILABLE = DNS.prototype.RECURSION_AVAILABLE = packet.RECURSION_AVAILABLE |
61
|
|
|
DNS.TRUNCATED_RESPONSE = DNS.prototype.TRUNCATED_RESPONSE = packet.TRUNCATED_RESPONSE |
62
|
|
|
DNS.AUTHORITATIVE_ANSWER = DNS.prototype.AUTHORITATIVE_ANSWER = packet.AUTHORITATIVE_ANSWER |
63
|
|
|
DNS.AUTHENTIC_DATA = DNS.prototype.AUTHENTIC_DATA = packet.AUTHENTIC_DATA |
64
|
|
|
DNS.CHECKING_DISABLED = DNS.prototype.CHECKING_DISABLED = packet.CHECKING_DISABLED |
65
|
|
|
|
66
|
|
|
DNS.prototype.address = function () { |
67
|
|
|
return this.socket.address() |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
DNS.prototype.bind = function (port, onlistening) { |
71
|
|
|
if (onlistening) this.once('listening', onlistening) |
|
|
|
|
72
|
|
|
this.socket.bind(port) |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
DNS.prototype.destroy = function (onclose) { |
76
|
|
|
if (onclose) this.once('close', onclose) |
|
|
|
|
77
|
|
|
if (this.destroyed) return |
|
|
|
|
78
|
|
|
this.destroyed = true |
79
|
|
|
clearInterval(this._interval) |
80
|
|
|
this.socket.close() |
81
|
|
|
for (var i = 0; i < this._queries.length; i++) { |
82
|
|
|
var q = this._queries[i] |
83
|
|
|
if (q) q.callback(new Error('Socket destroyed')) |
|
|
|
|
84
|
|
|
} |
85
|
|
|
this._queries = [] |
86
|
|
|
this._ids = [] |
87
|
|
|
this.inflight = 0 |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
DNS.prototype._ontimeout = function () { |
91
|
|
|
for (var i = 0; i < this._queries.length; i++) { |
92
|
|
|
var q = this._queries[i] |
93
|
|
|
if (!q) continue |
|
|
|
|
94
|
|
|
if (!q.tries.length) { |
95
|
|
|
this._queries[i] = null |
96
|
|
|
this._ids[i] = 0 |
97
|
|
|
this.inflight-- |
98
|
|
|
this.emit('timeout', q.query, q.port, q.host) |
99
|
|
|
q.callback(new Error('Query timed out')) |
100
|
|
|
continue |
101
|
|
|
} |
102
|
|
|
if (--q.tries[0]) continue |
|
|
|
|
103
|
|
|
q.tries.shift() |
104
|
|
|
this.socket.send(q.buffer, 0, q.buffer.length, q.port, q.host) |
105
|
|
|
} |
106
|
|
|
this._trim() |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
DNS.prototype._onmessage = function (buffer, rinfo) { |
110
|
|
|
try { |
111
|
|
|
var message = packet.decode(buffer) |
112
|
|
|
} catch (err) { |
113
|
|
|
this.emit('warning', err) |
114
|
|
|
return |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (message.type === 'response' && message.id) { |
118
|
|
|
var i = this._ids.indexOf(message.id) |
119
|
|
|
var q = i > -1 ? this._queries[i] : null |
120
|
|
|
if (q) { |
121
|
|
|
this.inflight-- |
122
|
|
|
this._ids[i] = 0 |
123
|
|
|
this._queries[i] = null |
124
|
|
|
this._trim() |
125
|
|
|
q.callback(null, message, q.query, rinfo.port, rinfo.address) |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
this.emit(message.type, message, rinfo.port, rinfo.address) |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
DNS.prototype._trim = function () { |
133
|
|
|
while (this._ids.length && !this._ids[this._ids.length - 1]) { |
134
|
|
|
this._ids.pop() |
135
|
|
|
this._queries.pop() |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
DNS.prototype.unref = function () { |
140
|
|
|
this.socket.unref() |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
DNS.prototype.ref = function () { |
144
|
|
|
this.socket.ref() |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
DNS.prototype.response = function (query, response, port, host) { |
148
|
|
|
if (this.destroyed) return |
|
|
|
|
149
|
|
|
|
150
|
|
|
response.type = 'response' |
151
|
|
|
response.id = query.id |
152
|
|
|
|
153
|
|
|
var buffer = packet.encode(response) |
154
|
|
|
this.socket.send(buffer, 0, buffer.length, port, host || '127.0.0.1') |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
DNS.prototype.cancel = function (id) { |
158
|
|
|
var i = this._ids.indexOf(id) |
159
|
|
|
var q = this._queries[i] |
160
|
|
|
if (!q) return |
|
|
|
|
161
|
|
|
|
162
|
|
|
this._queries[i] = null |
163
|
|
|
this._ids[i] = 0 |
164
|
|
|
this.inflight-- |
165
|
|
|
q.callback(new Error('Query cancelled')) |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
DNS.prototype.setRetries = function (id, retries) { |
169
|
|
|
var i = this._ids.indexOf(id) |
170
|
|
|
var q = this._queries[i] |
171
|
|
|
if (!q) return |
|
|
|
|
172
|
|
|
|
173
|
|
|
while (q.tries.length < retries) { |
174
|
|
|
q.tries.push(q.tries.length ? 2 * q.tries[q.tries.length - 1] : 4) |
175
|
|
|
} |
176
|
|
|
if (q.tries.length > retries) { |
177
|
|
|
q.tries = q.tries.slice(0, retries) |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
DNS.prototype.query = function (query, port, host, cb) { |
182
|
|
|
if (typeof host === 'function') return this.query(query, port, null, host) |
|
|
|
|
183
|
|
|
if (!cb) cb = noop |
|
|
|
|
184
|
|
|
|
185
|
|
|
if (this.destroyed) { |
186
|
|
|
nextTick(cb, new Error('Socket destroyed')) |
187
|
|
|
return 0 |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
this.inflight++ |
191
|
|
|
query.type = 'query' |
192
|
|
|
query.flags = typeof query.flags === 'number' ? query.flags : DNS.RECURSION_DESIRED |
193
|
|
|
var id = query.id = this._id++ |
194
|
|
|
if (this._id === 65535) this._id = 1 |
|
|
|
|
195
|
|
|
|
196
|
|
|
var i = this._ids.indexOf(0) |
197
|
|
|
if (i === -1) i = this._ids.push(0) - 1 |
|
|
|
|
198
|
|
|
if (this._queries.length === i) this._queries.push(null) |
|
|
|
|
199
|
|
|
|
200
|
|
|
var buffer = packet.encode(query) |
201
|
|
|
var tries = this._triesArray.slice(0) |
202
|
|
|
|
203
|
|
|
this._ids[i] = id |
204
|
|
|
this._queries[i] = { |
205
|
|
|
callback: cb, |
206
|
|
|
tries: tries, |
207
|
|
|
query: query, |
208
|
|
|
buffer: buffer, |
209
|
|
|
port: port, |
210
|
|
|
host: host |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
this.socket.send(buffer, 0, buffer.length, port, host || '127.0.0.1') |
214
|
|
|
return id |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
function noop () {} |
218
|
|
|
|
219
|
|
|
function nextTick (cb, err) { |
220
|
|
|
process.nextTick(function () { |
221
|
|
|
cb(err) |
222
|
|
|
}) |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
function add (a, b) { |
226
|
|
|
return a + b |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
function getTriesArray (retries) { |
230
|
|
|
var ret = [] |
231
|
|
|
if (retries <= 1) return ret |
|
|
|
|
232
|
|
|
for (var i = 1; i <= retries - 1; i++) { |
233
|
|
|
ret.push(Math.pow(2, i)) |
234
|
|
|
} |
235
|
|
|
return ret |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
function isListening (socket) { |
239
|
|
|
try { |
240
|
|
|
return socket.address().port !== 0 |
241
|
|
|
} catch (err) { |
242
|
|
|
return false |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.